class Solution {
public:
    bool isLetterMatch(string& s, string& p, int a, int b) {
        if (a == 0) return false;
        return s[a - 1] == p[b - 1] || p[b - 1] == '.';
    }
    bool isMatch(string s, string p) {
        int m = s.size();
        int n = p.size();
        vector<vector<bool>> res(m + 1, vector<bool>(n + 1));
        res[0][0] = true;
        // for (int i = 1; i <= m; ++i) {
        for (int i = 0; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (p[j - 1] == '*') {
                    res[i][j] = res[i][j] | res[i][j - 2];
                    if (isLetterMatch(s, p, i, j - 1)) {
                        res[i][j] = res[i][j] | res[i - 1][j];
                    }   
                }
                else {
                    if (isLetterMatch(s, p, i, j)) {
                        res[i][j] = res[i][j] | res[i - 1][j - 1];
                    }
                }
            }
        }
        return res[m][n];
    }
};